<template>
  <div style="margin-left: 20px;margin-top: 20px">
    <div class="container">
      <el-input class="input-field" style="width: 300px" v-model="input" placeholder="请输入内容"></el-input>
      <el-button class="input-field" type="primary" icon="el-icon-search" @click="queryByName(input)">查询</el-button>
      <el-button type="warning" icon="el-icon-circle-plus" @click="showFormxz">新增</el-button>

    </div>
    <br>
    <el-table :data="tableData" v-loading="loading" style="">
      <el-table-column prop="c_id" label="序号" v-if="showColumnid"></el-table-column>
      <el-table-column prop="c_name" label="指标名称"></el-table-column>
      <el-table-column prop="c_reference" label="正常参考值"></el-table-column>
      <el-table-column prop="c_unit" label="单位"></el-table-column>
      <el-table-column prop="c_sex" label="针对性别">
        <template v-slot="scope">
          {{ parseInt(scope.row.sex) === 0 ? '不限' : (parseInt(scope.row.sex) === 1 ? '男' : '女') }}
        </template>
      </el-table-column>
      <el-table-column prop="sex" label="操作">
        <!-- 使用v-for循环渲染按钮 -->
        <template v-slot="scope">
          <el-button type="primary" icon="el-icon-edit" @click="showEditForm(scope.row.c_id)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <br>

    <el-pagination style="float: right"
                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"
                   :current-page="currentPage"
                   :page-sizes="[5, 10, 15, 20]"
                   :page-size="pageSize"
                   layout="total, sizes, prev, pager, next, jumper"
                   :total="tableData.length"
    ></el-pagination>
    <el-dialog title="新增" :visible.sync="showForm">

      <el-form :inline="true" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="序号" prop="id">
              <el-input v-model="ruleForm.id"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="指标名称" prop="c_name">
              <el-input v-model="ruleForm.c_name"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="正常参考" prop="c_reference">
              <el-input v-model="ruleForm.c_reference"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="c_unit">
              <el-input v-model="ruleForm.c_unit"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="左临界值" prop="c_left_margin">
              <el-input v-model="ruleForm.c_left_margin"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="右临界值" prop="c_right_margin">
              <el-input v-model="ruleForm.c_right_margin"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="针对性别" prop="c_sex">
              <el-radio-group v-model="ruleForm.c_sex">
                <el-radio label="0">不限</el-radio>
                <el-radio label="1">男</el-radio>
                <el-radio label="2">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="c_status">
              <el-radio-group v-model="ruleForm.c_status">
                <el-radio label="0">启用</el-radio>
                <el-radio label="1">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="说明" prop="c_explanation">
              <el-input style="width: 400px" type="textarea" v-model="ruleForm.c_explanation"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item style="display: flex; justify-content: center;">
          <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog title="编辑" :visible.sync="showFormxg">
      <el-form :inline="true" :model="rulexgForm" :rules="rules" ref="rulexgForm" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="序号" prop="ids">
              <el-input v-model="rulexgForm.ids" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="指标名称" prop="c_name">
              <el-input v-model="rulexgForm.c_name"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="正常参考" prop="c_reference">
              <el-input v-model="rulexgForm.c_reference"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="c_unit">
              <el-input v-model="rulexgForm.c_unit"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="左临界值" prop="c_left_margin">
              <el-input v-model="rulexgForm.c_left_margin"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="右临界值" prop="c_right_margin">
              <el-input v-model="rulexgForm.c_right_margin"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="针对性别" prop="c_sex">
              <el-radio-group v-model="rulexgForm.c_sex">
                <el-radio label="0">不限</el-radio>
                <el-radio label="1">男</el-radio>
                <el-radio label="2">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="c_status">
              <el-radio-group v-model="rulexgForm.c_status">
                <el-radio label="0">启用</el-radio>
                <el-radio label="1">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="说明" prop="c_explanation">
              <el-input style="width: 400px" type="textarea" v-model="rulexgForm.c_explanation"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item style="display: flex; justify-content: center;">
          <el-button type="primary" @click="submitFormxg('rulexgForm')">确定</el-button>
          <el-button @click="resetFormxg('rulexgForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>

</template>
<style>

.container {
  display: flex;
  align-items: center;
}

.input-field {
  margin-right: 10px; /* 调整元素之间的间距 */

}

</style>
<script>
import row from 'element-ui/packages/row'
import axios from 'axios'
import table from '@/views/table/index.vue'

export default {
  beforeCreate() {
    axios.post("api/HealthPlan/queryIndicatorByName",{name: ""}).then(res => {
      console.log(res.data.data)
     this.tableData = res.data.data
      this.loading=false
    })
  },
  computed: {
    row() {
      return row
    }
  },
  data() {
    return {
      loading: true,
      input: '',
      currentPage: 1, // 当前页码
      pageSize: 5,   // 每页显示的条数
      activeName: 'first',
      showFormxg: false,//控制修改表单的显示
      showForm: false, // 控制添加表单的显示状态，默认隐藏
      showColumnid: true,//控制id数据的隐藏
      displayedData: [],//分页后的
      tableData: [],
      //修改用的对象
      rulexgForm: {
        ids: '',
        c_unit: '',
        c_name: '',
        c_reference: '',
        c_sex: '',
        c_status: '',
        c_left_margin: '',
        c_right_margin: '',
        c_explanation: ''
      },
      //添加用的对象
      ruleForm: {
        id: '',
        c_unit: '',
        c_name: '',
        c_reference: '',
        c_sex: '',
        c_status: '',
        c_left_margin: '',
        c_right_margin: '',
        c_explanation: ''
      },

      rules: {
        id: [
          { required: true, message: '请输入序号', trigger: 'blur' }
        ],
        c_unit: [
          { required: true, message: '请输入单位', trigger: 'blur' }
        ],
        c_name: [
          { required: true, message: '请输入指标名称', trigger: 'blur' }
        ],
        c_sex: [
          { required: true, message: '请选择针对性别', trigger: 'change' }
        ],
        c_explanation: [
          { required: true, message: '请输入说明信息', trigger: 'blur' }
        ],
        c_right_margin: [
          { required: true, message: '请输入右临界值信息', trigger: 'blur' }
        ],
        c_left_margin: [
          { required: true, message: '请输入左临界值信息', trigger: 'blur' }
        ],
        c_reference: [
          { required: true, message: '请填参考消息', trigger: 'blur' }
        ]
        ,
        c_status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      }

    }
  },
  methods: {
    referPage() {
      axios.post("api/HealthPlan/queryIndicatorByName",{name: ""}).then(res => {
        this.tableData = res.data.data
      })
    },
    // 当每页显示条数发生变化时触发
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      // 根据新的每页显示条数重新加载数据
      this.loadData()
    },
    queryByName(name){
      axios.post("api/HealthPlan/queryIndicatorByName",{name: name}).then(res => {
        this.tableData = res.data.data;
      })
    },

    // 当前页码发生变化时触发
    handleCurrentChange(val) {
      this.currentPage = val
      // 根据新的当前页码重新加载数据
      this.loadData()
    },
    // 根据当前页和每页显示的数量加载数据
    loadData() {
      const startIndex = (this.currentPage - 1) * this.pageSize//得到当前页从哪一页显示的
      const endIndex = startIndex + this.pageSize//得到下一页的页
      this.displayedData = this.tableData.slice(startIndex, endIndex)//（5，10）显示的是第二页的
      this.loading = false
    },
//确定添加
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 在验证通过后获取表单数据
          const formData = { ...this.ruleForm }
          console.log(formData)
          // 执行其他操作，例如发送表单数据到后端
          axios.post("api/HealthPlan/addIndicator", formData).then(res => {
            if(res.data.code === '200'){
              this.$message.success('添加成功!')
              this.$refs[formName].resetFields()
              this.showForm = false
              this.referPage()
            }
          })
        } else {
          alert('error submit!!')
          return false
        }
      })
    },
    //确定修改
    submitFormxg(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 在验证通过后获取表单数据
          const formData = { ...this.rulexgForm }
          console.log(formData)
          // 执行其他操作，例如发送表单数据到后端
          axios.post("api/HealthPlan/updateIndicator", formData).then(res => {
            if(res.data.code === '200'){
              this.$message.success('修改成功!')
              this.$refs[formName].resetFields()
              this.showFormxg = false
              this.referPage()
            }
          })
        } else {
          alert('error submit!!')
          return false
        }
      })
    },
    //显示添加页面
    showFormxz() {
      this.showForm = true
    },
    //显示修改页面
    showEditForm(id) {
      axios.post("api/HealthPlan/queryIndicatorById", {c_id: id }).then(res => {
        this.rulexgForm = res.data.data
        if(res.data.code === '200'){
          this.showFormxg = true
        }
      })
    },
    //添加页面取消
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.showForm = false
    },
    //修改页面取消
    resetFormxg(formName) {
      this.$refs[formName].resetFields()
      this.showFormxg = false
    }

  }
}

</script>
